package com.sunzm.dynamic_rule.mock_data;

import com.alibaba.fastjson.JSONObject;
import com.sunzm.dynamic_rule.pojo.RuleAtomicParam;
import scala.Long;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模拟规则数据插入MySQL
 */
public class MockRuleDataToMySQL {
    public static void main(String[] args) throws Exception {
        Connection conn = DriverManager.getConnection("jdbc:mysql://82.156.210.70:3306/rule_db?useSSL=false",
                "root", "ABC123abc.123");

        String ruleName = "rule2";

        JSONObject ruleJSON = new JSONObject();
        ruleJSON.put("ruleName", ruleName);
        JSONObject triggerParamJSON = new JSONObject();
        triggerParamJSON.put("eventId", "addCart");
        ruleJSON.put("triggerParam", triggerParamJSON);
        Map<String, String> userProfileMap = new HashMap<String, String>();
        userProfileMap.put("sex", "1");
        ruleJSON.put("userProfileParam", userProfileMap);
        List<RuleAtomicParam> userActionCountList = new ArrayList<>();
        RuleAtomicParam atomicParam1 = new RuleAtomicParam();
        atomicParam1.setEventId("addCart");
        atomicParam1.setThreshold(3);
        atomicParam1.setOriginStart(0L);
        atomicParam1.setOriginEnd(Long.MaxValue());
        atomicParam1.setCountQuerySql("select\n" +
                "    deviceId,\n" +
                "    count(1) as cnt\n" +
                "from event_detail\n" +
                "where deviceId = ? and eventId = 'addCart' and properties['sex']='1'\n" +
                "  and timeStamp between ? and ?\n" +
                "group by deviceId");
        userActionCountList.add(atomicParam1);
        ruleJSON.put("userActionCountParams", userActionCountList);
        List<RuleAtomicParam> userActionSequenceList = new ArrayList<>();
        RuleAtomicParam atomicParam2 = new RuleAtomicParam();
        atomicParam2.setEventId("addCart");
        atomicParam2.setOriginStart(0L);
        atomicParam2.setOriginEnd(Long.MaxValue());
        userActionSequenceList.add(atomicParam2);
        RuleAtomicParam atomicParam3 = new RuleAtomicParam();
        atomicParam3.setEventId("collect");
        atomicParam3.setOriginStart(0L);
        atomicParam3.setOriginEnd(Long.MaxValue());
        userActionSequenceList.add(atomicParam3);
        ruleJSON.put("userActionSequenceParams", userActionSequenceList);
        String ruleCode = ruleJSON.toJSONString();
        int ruleStatus = 1;
        String ruleType = "1";
        String ruleVersion = "1";
        String seqSqls = "SELECT\n" +
                "  deviceId,\n" +
                "  sequenceMatch('.*(?1).*(?2).*')(\n" +
                "    toDateTime(`timeStamp`),\n" +
                "    eventId = 'addCart',\n" +
                "    eventId = 'collect',\n" +
                "  ) as isMatch2,\n" +
                "  \n" +
                "  sequenceMatch('.*(?1).*')(\n" +
                "    toDateTime(`timeStamp`),\n" +
                "    eventId = 'addCart',\n" +
                "    eventId = 'collect',\n" +
                "  ) as isMatch1\n" +
                "\n" +
                "from event_detail\n" +
                "where  \n" +
                "  deviceId = ? \n" +
                "    and \n" +
                "  timeStamp >= ?\n" +
                "    and \n" +
                "  timeStamp <= ?\n" +
                "    and \n" +
                "  (\n" +
                "        (eventId='addCart')\n" +
                "     or (eventId = 'collect')\n" +
                "  )\n" +
                "group by deviceId;";
        String ruleCreator = "sunzm";
        String ruleAuditor = "sunzm.d";
        Date createTime = new Date(System.currentTimeMillis());
        Date updateTime = createTime;

        PreparedStatement pst = conn.prepareStatement("insert into canal_rule (rule_name,rule_code,rule_status," +
                "rule_type,rule_version,seq_sqls,rule_creator,rule_auditor,create_time,update_time) " +
                "values (?,?,?,?,?,?,?,?,?,?)");
        pst.setString(1,ruleName);
        pst.setString(2,ruleCode);
        pst.setInt(3,ruleStatus);
        pst.setString(4,ruleType);
        pst.setString(5,ruleVersion);
        pst.setString(6,seqSqls);
        pst.setString(7,ruleCreator);
        pst.setString(8,ruleAuditor);
        pst.setDate(9,createTime);
        pst.setDate(10,updateTime);

        int update = pst.executeUpdate();

        System.out.println("update: " + update);

        pst.close();
        conn.close();
    }
}
